#include "mainwindow.h"

#include <QSharedMemory>
#include <QApplication>

#include "Windows.h"

#pragma comment (lib, "User32.lib" )

const char *KEY_SHARED_MEMORY = "YourUniqueAppName";

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);

    QSharedMemory* sharedMemory = new QSharedMemory(KEY_SHARED_MEMORY);
    if(!sharedMemory->create(1)) {
        // 程序正在运行，提升到最前面来
        QString name = "Qt5152dQWindowIcon"; //  你自己的窗口的类名
        QString windowname = "MainWindow"; // 你自己的窗口标题名
        HWND hwnd = FindWindow((LPCWSTR)name.unicode(),(LPCWSTR)windowname.unicode());//查找窗口句柄

        if(::IsIconic(hwnd)) {
            ShowWindow(hwnd, SW_RESTORE);
        }
        ::SetForegroundWindow(hwnd);
        return -1; // the app is running
    }

    MainWindow w;
    w.show();

    int ret = a.exec();
    if (sharedMemory->isAttached())
    {
        sharedMemory->detach();
    }
    delete sharedMemory;
    sharedMemory = NULL;

    return ret;
}
